//
//  BuilderHelper.m
//  BehindTheMask
//
//  Created by Basuke on 08/03/16.
//  Copyright 2008 Basuke's. All rights reserved.
//

#import "BuilderHelper.h"


@implementation BuilderHelper

+ (UIButton *)buttonWithImage:(NSString *)imageName highlightImage:(NSString *)hiliteImageName at:(CGPoint)origin {
	UIImage *image = [UIImage imageNamed:imageName];
	
	CGRect frame;
	frame.origin = origin;
	if (image) {
		frame.size = image.size;
	} else {
		frame.size = CGSizeMake(16.0, 16.0);
	}
	UIButton *button = [[UIButton alloc] initWithFrame:frame];
	
	[button setImage:image forStates:UIControlStateNormal];
	
	image = [UIImage imageNamed:hiliteImageName];
	[button setImage:image forStates:UIControlStateHighlighted];
	
	return [button autorelease];
}

+ (UITextField *)urlFieldAt:(CGPoint)origin width:(CGFloat)width {
	CGRect frame;
	frame.origin = origin;
	frame.size = CGSizeMake(width, kBuilderHelper_StandardTextFieldHeight);
	
	UITextField *field = [[UITextField alloc] initWithFrame:frame];
	
	field.borderStyle = UITextFieldBorderStyleRounded;
//field.textColor = [UIColor blackColor];
	field.font = [UIFont systemFontOfSize: kBuilderHelper_SystemFontSize];
	field.placeholder = @"<enter a URL>";
	field.text = @"";
	field.backgroundColor = [UIColor whiteColor];
	
	field.clearButtonMode = UITextFieldViewModeWhileEditing;
	
	field.autoresizingMask = UIViewAutoresizingFlexibleWidth;
	field.returnKeyType = UIReturnKeyDone;
	field.keyboardType = UIKeyboardTypeURL;		// this makes the keyboard more friendly for typing URLs
	
	return [field autorelease];
}

+ (UIWebView *)webViewWithMarginTop:(CGFloat)top {
	return [BuilderHelper webViewWithMargin:UIEdgeInsetsMake(top, 0.0, 0.0, 0.0)];
}

+ (UIWebView *)webViewWithMargin:(UIEdgeInsets)inset {
	CGRect frame = [[UIScreen mainScreen] applicationFrame];
	frame.origin.x += inset.left;
	frame.origin.y += inset.top;
	frame.size.width -= inset.left + inset.right;
	frame.size.height -= inset.top + inset.bottom;
	
	UIWebView *webView = [[UIWebView alloc] initWithFrame:frame];
	webView.backgroundColor = [UIColor whiteColor];
	webView.scalesPageToFit = YES;
	webView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
	
	return [webView autorelease];
}

+ (UILabel *)labelWith:(NSString *)aLabel at:(CGPoint)origin withFont:(UIFont *)font  {
	CGRect frame = [[UIScreen mainScreen] applicationFrame];
	frame.origin = origin;
	frame.size.width -= frame.origin.x;
	frame.size.height = [font ascender] + [font descender];
	
	UILabel *label = [[[UILabel alloc] initWithFrame:frame] autorelease];
	label.text = aLabel;
	label.font = font;
	return label;
}

+ (UILabel *)labelWith:(NSString *)aLabel at:(CGPoint)origin {
	return [BuilderHelper labelWith:aLabel at:origin 
						   withFont:[UIFont systemFontOfSize: [UIFont smallSystemFontSize]]];
}

+ (UILabel *)titleLabelWith:(NSString *)aLabel at:(CGPoint)origin {
	return [BuilderHelper labelWith:aLabel at:origin 
						   withFont:[UIFont boldSystemFontOfSize: [UIFont smallSystemFontSize]]];
}

@end

@implementation UIView(BuilderHelper)

- (UIView *)moveTo:(CGPoint)pt {
	return [self moveTo:pt anchor:kBuilderHelperAncherToLeftTop];
	
}

- (UIView *)moveTo:(CGPoint)pt anchor:(NSUInteger)anchorCorner {
	CGRect r = [self frame];
	r.origin = pt;
	self.frame = r;
	return self;
}

- (CGPoint)rightBottom {
	CGRect r = [self frame];
	return CGPointMake(CGRectGetMaxX(r), CGRectGetMaxY(r));
}

@end

